import {queryPresentRecordsPage,
        createPresentRecord,
        updatePresentRecord,
        deletePresentRecord,
        getPresentRecord
    } from '../../services/capital/cash';
import {message} from 'antd';
import Constants from "../../utils/Constants";
const R = require('ramda');

export default {
    namespace: 'cash',
    state: {
        list: [],
        page: {},
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         * @param {*} state 
         * @param {*} param1 
         */
        refreshState(state, { payload }) {
            return {...state, ...payload};
        }
    },
    effects: {
        /**
         * 分页查询提现申请
         *
         * @param call
         * @param put
         * @return
         */
        *queryPresentRecordsPage({ payload }, { call, put }){
            const res = yield call(queryPresentRecordsPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                yield put({
                    type:'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                if(payload.onFail) payload.onFail();
            }
        },
        /**
         * 新增提现申请
         *
         * @param call
         * @return
         */
        *createPresentRecord({payload}, {call}){
            const res = yield call(createPresentRecord, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if(res && res.data.resultCode === "ok"){
                message.info('提现申请新增成功!');
                if(payload.onSuccess) payload.onSuccess();
            }else{
                if(payload.onFail) payload.onFail();
            }
        },

         /**
         * 更新提现申请
         *
         * @param call
         * @return
         */
        *updatePresentRecord({payload}, {call}){
            const res = yield call(updatePresentRecord, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if(res && res.data.resultCode === "ok"){
                message.info('提现申请更新成功!');
                if(payload.onSuccess) payload.onSuccess();
            }else{
                if(payload.onFail) payload.onFail();
            }
        },

        /**
         * 删除提现申请
         *
         * @param call
         * @return
         */
        *deletePresentRecord({payload}, {call}){
            const res = yield call(deletePresentRecord, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if(res && res.data.resultCode === "ok"){
                message.info('提现申请删除成功!');
                if(payload.onSuccess) payload.onSuccess();
            }else{
                if(payload.onFail) payload.onFail();
            }
        },

        /**
         * 按id查看提现申请
         *
         * @param call
         * @return
         */
        *getPresentRecord({payload}, {call, put}){
            const res = yield call(getPresentRecord, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if(payload.id){
                if(res && res.data.resultCode === "ok"){
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    })
                }else{
                    if(payload.onFail) payload.onFail();
                }
            }else{
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                })
            }
        },

    },
}